package mfund.com.controller;

import java.util.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import mfund.com.service.*;
import mfund.com.model.*;
import javax.annotation.Resource;



@Controller
public class TestController
{
	@Resource
	private ProductService productService;

	@RequestMapping(value="number",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody int getNumber()
	{
		System.out.println("this is the number");

		return 2;
	}


	@RequestMapping(value="map",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody MFResult getMap()
	{
		System.out.println("this is the map");
		MFResult map=new MFResult();
		/*Map<String,Object> map=new ConcurrentHashMap<String,Object>();*/
		/*map.put("status","successs");
		map.put("message","this is the message");*/
		List<String> arr=new ArrayList<String>();
		arr.add("1111");
		arr.add("2222");
		
		map.setStatusFail();
		map.setStatusSuccess();
		

		List<TProduct> products= productService.query();

		map.setData(products);

		return map;
	}

	@RequestMapping(value="path/{pathvar}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Map<String,Object> getPath(@PathVariable String pathvar,@RequestParam(required=false) String type)
	{
		System.out.println("this is the path"+type+pathvar);

		Map<String,Object> map=new ConcurrentHashMap<String,Object>();
		map.put("status","successs");
		map.put("message",pathvar);

		return map;
	}

	@RequestMapping(value="fileUpload",method=RequestMethod.POST)
	public @ResponseBody String fileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request)
	{
		System.out.println("this is fileUpload");
		if(!file.isEmpty()){
			try{
				String filepath=request.getSession().getServletContext().getRealPath("/")+"upload/"
							+file.getOriginalFilename();
				file.transferTo(new File(filepath));
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			
		}
		return "";
	}

}